package com.example.final_project.Usr.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.MainView;
import com.example.final_project.R;

public class ReleaseActivity extends AppCompatActivity {

    private Context context;
    private ImageView back;
    private EditText title,content;
    private Button release;
    public static final int RELEASE=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release);
        Intent intent = this.getIntent();
        findById();
        String name = intent.getStringExtra("name");
        String title1 = title.getText().toString();

        //发布事件
        release.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content1 = content.getText().toString();
                release(name,content1);
            }
        });

        //返回点击事件
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(ReleaseActivity.this, MainView.class);
                intent1.setAction("backToUserFrag");
                startActivity(intent1);
            }
        });

    }

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case RELEASE:
                    Boolean check = (Boolean)msg.obj;
                    if (check){
                        Toast.makeText(context,"发布成功",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(ReleaseActivity.this,MainView.class);
                        intent.setAction("backToUserFrag");
                        startActivity(intent);
                    }else {
                        Toast.makeText(context,"发布失败",Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };

    public void release(String name,String content){
        OkHttps.async("http://10.150.11.7:8086/chengcar/releasecontent/")
                .bind(context)
                .addUrlPara("name",name)
                .addUrlPara("content",content)
                .setOnResBean(Boolean.class,check ->{
                    Message message = Message.obtain();
                    message.what=RELEASE;
                    message.obj=check;
                    handler.sendMessage(message);
                })
                .get();
    }

    public void findById(){
        context = getBaseContext();
        back = this.findViewById(R.id.release_back);
        title = this.findViewById(R.id.release_title);
        content = this.findViewById(R.id.release_content);
        release = this.findViewById(R.id.release_release);
    }

}